package com.sikaryofficial.information.message.consumer;

import com.sikaryofficial.information.constant.ArticleDataSyncBusinessTypeEnum;
import com.sikaryofficial.information.message.body.InformationMessage;
import com.sikaryofficial.information.service.articlesync.DataSyncStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class ArticleDataSyncProcess {

    @Resource
    private Map<String, DataSyncStrategy> dataSyncStrategyMap;

    /**
     * 消息监听
     * <p>
     * 抢购业务-消费流程
     *
     * @param message 消息
     */
    @StreamListener(MQConsumer.BINDINGS_INFORMATION_SYNC_INPUT)
    public void onInformationSyncMessage(@Payload InformationMessage message) {
        log.info("=========start=============################====");
        log.info("[onMessage][线程编号:{} ]", Thread.currentThread().getId());
        log.info("==========================###################====");
        ArticleDataSyncBusinessTypeEnum typeEnum = ArticleDataSyncBusinessTypeEnum.getNameByCode(message.getBusinessType());
        if (Objects.isNull(typeEnum)) {
            log.error("typeEnum is not exist");
            return;
        }
        try {
            DataSyncStrategy pointsDetailStrategy = dataSyncStrategyMap.get(typeEnum.getValue() + DataSyncStrategy.SUFFIX);
            pointsDetailStrategy.processSync(message);
        }catch (Exception e){
            log.error("processSync error", e);
        }
        log.info("=========end================################====");
    }
}
